package cn.kgc.controller;

import cn.kgc.entity.Order;
import cn.kgc.entity.Shop;
import cn.kgc.entity.UserB;
import cn.kgc.service.userService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author 蒿雨
 * @create 2021-11-15 10:14
 */
@Controller
@RequestMapping("/tj")
public class TjController {

    @Resource
    private userService userService;

    @RequestMapping("/faceLogin")
    public String faceLogin(String name, HttpSession session) {
        UserB user = userService.getUserg(name);
        session.setAttribute("user", user);
        return "redirect:/tj/userCenter";
    }


    @RequestMapping(value = "/web")
    public String web(String name) {
        return "redirect:http://localhost:8089/demo&name=" + name;
    }


    @RequestMapping("/checkUserCenter")
    @ResponseBody
    public String checkUserCenter(HttpSession session) {
        UserB userB = (UserB) session.getAttribute("user");
        if (userB == null) {
            return JSON.toJSONString("false");
        } else {
            return JSON.toJSONString("true");

        }
    }

    @RequestMapping("/userCenter")
    public String touserCenter(HttpSession session) {
        UserB userB = (UserB) session.getAttribute("user");
        UserB userB1 = userService.getUser(userB.getUserName(), userB.getPwd());
        session.setAttribute("user", userB1);

        userB = userB1;

        if (userB != null) {
            return "userCenter";
        }
        return "index";
    }

    @RequestMapping("/downld")
    public String q(HttpServletResponse response, HttpServletRequest request) throws IOException {
        //获得请求文件名
        //String filename = request.getParameter("filename");
        String filename = "zuhaowan.zip";
        //System.out.println(filename);
        //设置文件MIME类型
        response.setContentType(request.getSession().getServletContext().getMimeType(filename));
        //设置Content-Disposition
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        //读取目标文件，通过response将目标文件写到客户端
        //获取目标文件的绝对路径
        //String fullFileName = getServletContext().getRealPath("/download/" + filename);

        String fullFileName = request.getSession().getServletContext().getRealPath("/statics/download/" + filename);
        //System.out.println(fullFileName);
        //读取文件
        InputStream in = new FileInputStream(fullFileName);
        OutputStream out = response.getOutputStream();
        //写文件
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        in.close();
        out.close();

        return "index";
    }

    @RequestMapping("/center_myOrders")
    public ModelAndView center_myOrders(HttpSession session, Integer pageNumstr) {//查看我的订单
        ModelAndView modelAndView = new ModelAndView("center_orders");
        UserB userB = (UserB) session.getAttribute("user");
        Integer uid = userB.getId();
        Integer page = 1;
        if (pageNumstr != null) {
            page = Integer.valueOf(pageNumstr);
        }
        Integer pageSize = 5;
        PageHelper.startPage(page, pageSize);
        System.out.println(123);
        List<Order> orderList = userService.getSCOrderList(uid);
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        modelAndView.addObject(pageInfo);
        return modelAndView;
    }


    @RequestMapping("/center_car")
    public ModelAndView center_car(HttpSession session, Integer pageNumstr) {
        ModelAndView modelAndView = new ModelAndView("center_car");
        UserB userB = (UserB) session.getAttribute("user");
        Integer uid = userB.getId();
        Integer page = 1;
        if (pageNumstr != null) {
            page = pageNumstr;
        }
        Integer pageSize = 5;
        PageHelper.startPage(page, pageSize);
        List<Shop> shopList = userService.getSCShopList(uid);
        PageInfo<Shop> pageInfo = new PageInfo<>(shopList);
        modelAndView.addObject(pageInfo);
        return modelAndView;
    }

    @RequestMapping("/alert")
    public String alert() {
        return "alert";
    }

    @RequestMapping("/show")
    public String show() {
        return "show";
    }

    @RequestMapping("/vip")
    public String vip() {
        return "vip";
    }


    @RequestMapping("/jm")
    public String jm() {
        return "jm";
    }


    @RequestMapping("/index")
    public String index() {
        return "index";
    }


    @RequestMapping("/center_yfb")
    public String center_yfb() {
        return "center_yfb";
    }

    @RequestMapping("/center_fb")
    public String center_fb() {
        return "center_fb";
    }


    @RequestMapping("/toselect")
    public String toselect() {
        return "select";
    }

    @RequestMapping("/alipaytradepagepayjsp")
    public String zf() {
        return "alipay.trade.page.pay";
    }


    @RequestMapping("/login")
    public String tologin() {
        return "login";
    }


    @RequestMapping("/register")
    public String toregister() {
        return "register";
    }

    @RequestMapping("/cart")
    public String GetCart() {
        return "center_car";
    }


}
